-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add #_objectFileFormat compilation conditional #80212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@swift-ci please test |
lib/Basic/LangOptions.cpp
Outdated
// Set the "_objectFileFormat" platform condition. | ||
if (Target.isOSBinFormatMachO()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "MachO"); | ||
} else if (Target.isOSBinFormatELF()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "ELF"); | ||
} else if (Target.isOSBinFormatCOFF()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF"); | ||
} else if (Target.isOSBinFormatWasm()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rewrite this as:
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
llvm::StringSwitch<llvm::Triple::ObjectFormatType>(Target.getObjectFormat())
.Case(llvm::Triple::COFF, "COFF")
.Case(llvm::Triple::ELF, "ELF")
.Case(llvm::Triple::MachO, "MachO")
.Case(llvm::Triple::Wasm, "Wasm"));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not really, because we're not switching over strings here, but over the enum cases... I could do a regular switch, but that reads worse than the chain of ifs:
StringRef FileFormatName;
switch (Target.getObjectFormat()) {
case llvm::Triple::COFF:
FileFormatName = "COFF";
break;
case llvm::Triple::ELF:
FileFormatName = "ELF";
break;
case llvm::Triple::MachO:
FileFormatName = "MachO";
break;
case llvm::Triple::Wasm:
FileFormatName = "Wasm";
break;
default:
break;
}
if (!FileFormatName.empty()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
FileFormatName);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The covered switch would make it easier to identify new cases though I think. It is more that I am after a switch rather than a series of cascading if
conditions.
Should we consider gating this behind an experimental feature flag? |
I'm happy to do that if we deem that necessary, though all the other existing not-yet-ratified compilation conditions (e.g. pointerBitWidth) don't do that and just use an underscore. |
@swift-ci please test |
Swift Testing will need to make use of this to support Embedded Swift, and requiring an experimental flag will make it more difficult to do so. (When it gets formally featurized, we can eagerly adopt the final version.) |
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF"); | ||
} else if (Target.isOSBinFormatWasm()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add an else
clause here that diagnoses the target is unknown? That way if we add a new port, we'll know to update this code.
ea66110
to
4b27ab7
Compare
@swift-ci please test |
@swift-ci please test |
4b27ab7
to
7ee3e63
Compare
@swift-ci please test |
@swift-ci please test |
7ee3e63
to
5544dc5
Compare
@swift-ci please test |
5544dc5
to
65a496f
Compare
@swift-ci please test |
Are there any other object formats that LLVM supports? Should we add cases for them even if Swift itself hasn't added toolchain support? Edit: The following are all the cases in the object format enum in the Swift repo: enum ObjectFormatType {
UnknownObjectFormat,
COFF,
DXContainer,
ELF,
GOFF,
MachO,
SPIRV,
Wasm,
XCOFF,
}; So it may make sense to just switch over the whole enum (with |
@swift-ci please test |
I think it's probably undesirable to go beyond the 4 object file formats that Swift targets today, because (1) we'd have to lock down the specific spelling of those and (2) it would suggest that the compiler is actually capable of producing valid object files in these formats, but that's almost certainly not true until someone actively builds that support. |
@swift-ci please test |
@swift-ci please test macOS platform |
@swift-ci please test |
I don't know if I agree here. However, we do in a number of places currently just abort if you use a different object file format. Some of these are unlikely to ever be supported (e.g. DXContainer and SPIRV), so adding the full coverage really doesn't add much value, but does add noise. |
This implements the relevant part of the "Section Placement" proposal (draft: https://github.com/kubamracek/swift-evolution/blob/section-placement-control/proposals/0nnn-section-control.md, forum thread: https://forums.swift.org/t/pitch-3-section-placement-control/77435). Namely the ability to conditionalize on the object file format that the compiler is targeting (MachO, ELF, COFF of Wasm).
Adding with an underscored name (
#_objectFileFormat
) until the proposal is codified.rdar://147505228